/**
 * MIT License
 *
 * Copyright (c) 2024 小村程序员
 * Email: 397237738@qq.com
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.open.cobertura.params;


import org.open.cobertura.model.AgentOption;
import org.open.cobertura.model.OptionEnum;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AgentBuilder extends AbstractAgentBuilder {
    private List<AgentOption> includes;
    private List<AgentOption> excludes;
    /**
     * 参数集合
     */
    private static Map<String,AgentOption> options=new HashMap<>();

    public AgentBuilder() {
        this.includes=new ArrayList<>();
        this.excludes=new ArrayList<>();
    }
    @Override
    public void parseAgentArgs(String agentArgs){
        String[] agentParams=agentArgs.split(",");
        for(int i=0;i<agentParams.length;i++){
            String agentParam=agentParams[i];
            String[] options=agentParam.split("=");
            validation(options[0]);
            parseIncludesRule(options);
            parseExcludesRule(options);
            parsePort(options);
        }
    }
    @Override
    protected void parseExcludesRule(String optionValue){
        String[] rules=optionValue.split(":");
        for(String rule:rules){
            AgentOption agentOption=new AgentOption(OptionEnum.EXCLUDES.getName(),rule);
            excludes.add(agentOption);
        }
    }

    @Override
    protected void parseIncludesRule(String optionValue){
        String[] rules=optionValue.split(":");
        for(String rule:rules){
            AgentOption agentOption=new AgentOption(OptionEnum.INCLUDES.getName(),rule);
            includes.add(agentOption);
        }
    }

    protected void parsePort(String optionValue){
        String[] ports=optionValue.split(":");
        for(String port:ports){
            AgentOption agentOption=new AgentOption(OptionEnum.PORT.getName(),port);
            options.put(OptionEnum.PORT.getName(),agentOption);
        }
    }

    @Override
    public List<AgentOption> getIncludes() {
        return this.includes;
    }

    @Override
    public List<AgentOption> getExcludes() {
        return this.excludes;
    }

    /**
     * 得到端口选项
     * @return
     */
    public static int getPortOption(){
       String port= options.get(OptionEnum.PORT.getName()).getValue();
       return Integer.parseInt(port);
    }

    public static void main(String[] args) {
        AbstractAgentBuilder abstractAgentBuilder=new AgentBuilder();
        abstractAgentBuilder.parseAgentArgs("includes=com.youfan.*:com.opentester.*,output=tcpserver,port=6300");
        abstractAgentBuilder.isInclude("com/youfan/Demo");
    }
}
